import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

public class UpgradeScreen{
	private int screenWidth;
	private int screenHeight;
	private Shape foodIncrement;
	private Shape foodDecrement;
	private Shape gridIncrement;
	private Shape gridDecrement;
	private Drawer d;
	private Environment e;
	public UpgradeScreen(Drawer d, int screenWidth, int screenHeight, Environment e){
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		this.d=d;
		this.e=e;
	}
	
	public Graphics2D paint(Graphics2D gimage){
		
		//Drawing the background
		gimage.setColor(new Color(0,12,58));
		gimage.fillRect(0, 0, screenWidth, screenHeight);
		
		//Drawing the "Upgrades" text at the top
		gimage.setColor(new Color(255,255,255));
		gimage.setFont(d.getTitleText());
		gimage = drawCenteredString(gimage,"UPGRADES",screenWidth/2,screenHeight/8);
		
		//Drawing the food amount upgrade section
		foodDecrement = new Rectangle(screenWidth/8,7*screenHeight/8-20+40/3,40, 40/3);
		foodIncrement = drawPlus(screenWidth/8+100,7*screenHeight/8-20,40);
		if(e.getSnake().getFoodNumber()!=1){
			gimage.draw(foodDecrement);
		}
		if(e.getSnake().getFoodNumber()!=e.getSnake().getMaxFoodNumber()){
			gimage.draw(foodIncrement);
		}
		gimage.setFont(d.getSmallText());
		drawCenteredString(gimage, "FOOD AMOUNT", screenWidth/8+70, 13*screenHeight/16);
		drawCenteredString(gimage, String.valueOf(e.getSnake().getFoodNumber()), screenWidth/8+70, 7*screenHeight/8);
		gimage.setFont(d.getUpgradeText());
		gimage.setColor(new Color(20,170,20));
		if(e.getSnake().getFoodUpgradeLevel()<e.getSnake().getMaxFoodNumber() && e.getSnake().getFoodNumber()==e.getSnake().getFoodUpgradeLevel())
			drawCenteredString(gimage, String.valueOf(e.getSnake().getFoodUpgradeCost()), screenWidth/8+120,7*screenHeight/8+2); 

		//Drawing the gridSize upgrade section
		gridDecrement = new Rectangle(7*screenWidth/8-140,7*screenHeight/8-20+40/3,40, 40/3);
		gridIncrement = drawPlus(7*screenWidth/8-40,7*screenHeight/8-20,40);
		gimage.setColor(new Color(255,255,255));
		if(e.getSnake().getGridSize()!=e.getSnake().getMinGridSize()){
			gimage.draw(gridDecrement);
		}
		if(e.getSnake().getGridSize()!=e.getSnake().getMaxGridSize()){
			gimage.draw(gridIncrement);
		}
		gimage.setFont(d.getSmallText());
		drawCenteredString(gimage, "GRID SIZE", 7*screenWidth/8-70, 13*screenHeight/16);
		drawCenteredString(gimage, String.valueOf(e.getSnake().getGridSize()), 7*screenWidth/8-70, 7*screenHeight/8);
		gimage.setFont(d.getUpgradeText());
		gimage.setColor(new Color(20,170,20));
		if(e.getSnake().getGridSize()>e.getSnake().getMinGridSize())
			drawCenteredString(gimage, String.valueOf(e.getSnake().getGridUpgradeCost()), 7*screenWidth/8-120,7*screenHeight/8+2); 

		
		//Drawing the amount of points at the bottom
		gimage.setFont(d.getSmallText());
		gimage.setColor(new Color(20,170,20));
		drawCenteredString(gimage, String.valueOf("Points: " + e.getSnake().getPoints()), screenWidth/2, 15*screenHeight/16);
		
		return gimage;
	}
	
	private Graphics2D drawCenteredString(Graphics2D gimage, String str,int x, int y){
		FontMetrics fm = gimage.getFontMetrics();
		Rectangle2D rect = fm.getStringBounds(str, gimage);
		gimage.drawString(str, (int) (x-rect.getWidth()/2), (int) (y+rect.getHeight()/4));
		return gimage;
	}
	
	//Draws a plus at the desired location, with a line width of 1/6 the side length
	private Shape drawPlus(int x1, int y1, int sideLength){
		int drawWidth = 3;
		int topY = (y1+sideLength/2)-(sideLength/(drawWidth*2));
		int bottomY = (y1+sideLength/2)+sideLength/(drawWidth*2);
		int leftX = (x1+sideLength/2)-sideLength/(drawWidth*2);
		int rightX = (x1+sideLength/2)+sideLength/(drawWidth*2);
		int farRight = x1+sideLength;
		int farBottom = y1+sideLength;
		int[] coords = {leftX,y1,
				rightX,y1,
				rightX,topY,
				farRight,topY,
				farRight,bottomY,
				rightX,bottomY,
				rightX,farBottom,
				leftX,farBottom,
				leftX,bottomY,
				x1,bottomY,
				x1,topY,
				leftX,topY};
		
		return createPolygon(coords);
	}
	
	private Shape createPolygon(int[] coords){
		if(coords.length%2!=0){
			System.out.println("Must be even number of coords to create Polygon");
			return null;
		}
		int[] xcoords = new int[coords.length/2];
		int[] ycoords = new int[coords.length/2];
		for(int i=0;i<coords.length;i++){
			if(i%2==0){
				xcoords[i/2]=coords[i];
			}else{
				ycoords[i/2]=coords[i];
			}
		}
		
		return new Polygon(xcoords,ycoords,xcoords.length);
	}

	/**
	 * @return the foodIncrement
	 */
	public Shape getFoodIncrement() {
		return foodIncrement;
	}

	/**
	 * @param foodIncrement the foodIncrement to set
	 */
	public void setFoodIncrement(Shape foodIncrement) {
		this.foodIncrement = foodIncrement;
	}

	/**
	 * @return the foodDecrement
	 */
	public Shape getFoodDecrement() {
		return foodDecrement;
	}

	/**
	 * @param foodDecrement the foodDecrement to set
	 */
	public void setFoodDecrement(Shape foodDecrement) {
		this.foodDecrement = foodDecrement;
	}

	/**
	 * @return the gridIncrement
	 */
	public Shape getGridIncrement() {
		return gridIncrement;
	}

	/**
	 * @param gridIncrement the gridIncrement to set
	 */
	public void setGridIncrement(Shape gridIncrement) {
		this.gridIncrement = gridIncrement;
	}

	/**
	 * @return the gridDecrement
	 */
	public Shape getGridDecrement() {
		return gridDecrement;
	}

	/**
	 * @param gridDecrement the gridDecrement to set
	 */
	public void setGridDecrement(Shape gridDecrement) {
		this.gridDecrement = gridDecrement;
	}
}
